package cn.sharp.bbs.controller;

import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.common.TokenUtils;
import cn.sharp.bbs.entity.BO.CurrentUser;
import cn.sharp.bbs.service.CommonService;
import cn.sharp.bbs.service.FlowService;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 处理帖子流
 * @author zhao yu
 * @version 0.0.1
 */
@RestController
@RequestMapping("/flow")
public class FlowController {

    @Resource
    private FlowService flowService;

    @Resource
    private CommonService commonService;

    @RequestMapping("/{area}/{type}/{label}")
    public Result getFlow(@PathVariable("area") int area,
                          @PathVariable("type") int type,
                          @PathVariable("label") int label,
                          @Nullable @RequestAttribute("CurrentUser") CurrentUser user){
        return Result.success(flowService.getFlow(area, type, label, user));
    }

    /**
     * 获得最新的帖子流
     * @return 封装的帖子列表
     */
    @RequestMapping("/latest")
    public Result getLatestFlow(@Nullable @RequestAttribute("CurrentUser") CurrentUser user){
        return Result.success(flowService.getLatestFlow(user));
    }

    /**
     * 获得最热的帖子流
     * @return 封装的帖子列表
     */
    @RequestMapping("/hottest")
    public Result getHottestFlow(@Nullable @RequestAttribute("CurrentUser") CurrentUser user){
        return Result.success(flowService.getHottestFlow(user));
    }

    /**
     * 获得关注用户的帖子流
     * @param token 令牌
     * @return 封装的帖子列表
     */
    @RequestMapping("/byFollow")
    public Result getFlowByFollow(@RequestHeader("Token") String token){
        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);
        return Result.success(flowService.getFlowByFollow(currentUser));
    }

    /**
     * 获得用户的个性化推荐帖子流
     * @param user 当前用户
     * @return 封装的帖子列表
     */
    @RequestMapping("/personalized")
    public Result getPersonalizedFlow(@RequestAttribute("CurrentUser") CurrentUser user){
        return Result.success(flowService.getPersonalizedFlow(user));
    }

    /**
     * 获得标签列表
     */
    @GetMapping("/labelList")
    public Result getLabelList(){
        return Result.success(flowService.getLabelList());
    }
}
